package rtprtcp

import (
	"time"
	
	"github.com/bluenviron/gortsplib/v4/pkg/ringbuffer"
	"github.com/bluenviron/gortsplib/v4/pkg/rtcpreceiver"
	"github.com/bluenviron/gortsplib/v4/pkg/rtcpsender"
	"github.com/bluenviron/gortsplib/v4/pkg/rtplossdetector"
	"github.com/bluenviron/gortsplib/v4/pkg/rtptime"
	"github.com/pion/rtcp"
)

type (
	RtpReorder      = reorder                      // 重新排序和丢包检测(UDP)
	RtpLossDetector = rtplossdetector.LossDetector // 丢包检测(TCP)
)

type (
	RtpTimeEncoder        = rtptime.Encoder       // rtp time encoder
	RtpTimeSessionDecoder = rtptime.GlobalDecoder // session rtp time decoder
)

type (
	RTCPReceiver = rtcpreceiver.RTCPReceiver // rtcp receiver
	RTCPSender   = rtcpsender.RTCPSender     // rtcp sender
)

type (
	RingBuffer = ringbuffer.RingBuffer // 循环缓存队列
)

// NewRtpReorder rtp重新排序和丢包检测(UDP)
func NewRtpReorder(bufferSize int) *RtpReorder {
	return newReorder(bufferSize)
}

// NewRtpLossDetector rtp丢包检测(TCP)
func NewRtpLossDetector() *RtpLossDetector {
	return rtplossdetector.New()
}

// NewRtpTimeEncoder rtp time encoder
func NewRtpTimeEncoder(clockRate int, initialTimestamp uint32) *RtpTimeEncoder {
	return rtptime.NewEncoder(clockRate, initialTimestamp)
}

// NewRtpTimeSessionDecoder session rtp time decoder
func NewRtpTimeSessionDecoder() *RtpTimeSessionDecoder {
	return rtptime.NewGlobalDecoder()
}

// NewRTCPReceiver rtcp receiver
func NewRTCPReceiver(
	clockRate int, receiverSSRC *uint32, period time.Duration,
	timeNow func() time.Time,
	writePacketRTCP func(rtcp.Packet),
) (*RTCPReceiver, error) {
	return rtcpreceiver.New(clockRate, receiverSSRC, period, timeNow, writePacketRTCP)
}

// NewRTCPSender rtcp sender
func NewRTCPSender(
	clockRate int, period time.Duration,
	timeNow func() time.Time,
	writePacketRTCP func(rtcp.Packet),
) *RTCPSender {
	return rtcpsender.New(clockRate, period, timeNow, writePacketRTCP)
}

// NewRingBuffer 循环缓存队列
func NewRingBuffer(size uint64) (*RingBuffer, error) {
	return ringbuffer.New(size)
}
